//安装第三方模块 npm install mysql,引入数据库模块

const mysql = require("mysql");

var con = mysql.createConnection({
    host: '192.168.16.144', //虚拟机的IP
    port: 3306, //默认端口3306
    user: 'root',
    password: 'ddd',
    database: 'mydatabase' //链接的数据库名
})
var userName = process.argv[2];
var age = process.argv[3];
var sex = process.argv[4];

var insertSql = "insert into users values(?, ?, ?, ?)"; //'?'参数化查询，避免接收到的数据直接拼接到SQL语句中
var primarykey = (new Date()).getTime();
con.query(insertSql, [primarykey, userName, age, sex], (err, result)=>{ 
        // 4个参数：该条语句的ID值，SQL语句，插入的语句，回调函数
    if(err){
        console.log(err)
    }
    else{
        console.log(result)
    }
})